package com.xuelang.pipeline.artifact.infrastructure.resource.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xuelang.pipeline.artifact.client.common.constant.Constant;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.resource.dto.ArtifactUserQuery;
import com.xuelang.pipeline.artifact.domain.resource.ArtifactUser;
import com.xuelang.pipeline.artifact.domain.resource.repository.ArtifactUserRepository;
import com.xuelang.pipeline.artifact.infrastructure.resource.converter.ArtifactUserToArtifactUserDOConverter;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.ArtifactAttributeDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.ArtifactUserDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.mapper.ArtifactUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 仓库用户仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class ArtifactUserRepositoryImpl implements ArtifactUserRepository {

    @Autowired
    ArtifactUserMapper artifactUserMapper;

    @Override
    public PageRecord<ArtifactUser> pageList(long currentPage, long pageSize, ArtifactUserQuery artifactUserQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<ArtifactUser> page = new Page<>(currentPage, pageSize);
        MPJLambdaWrapper<ArtifactUserDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(ArtifactUserDO.class)
                .eq(ArtifactUserDO::getArtifactUuid, StrUtil.isNotBlank(artifactUserQuery.getArtifactUuid()) ? artifactUserQuery.getArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);
        wrapper.and(StrUtil.isNotBlank(artifactUserQuery.getCondition()), wp -> wp
                        .like(ArtifactUserDO::getArtifactUserName, artifactUserQuery.getCondition()).or()
                        .like(ArtifactUserDO::getArtifactRoleCode, artifactUserQuery.getCondition()).or()
                        .like(ArtifactUserDO::getArtifactType, artifactUserQuery.getCondition()))
                .apply(StrUtil.isNotBlank(artifactUserQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactUserQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactUserQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactUserQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        artifactUserMapper.selectJoinPage(page, ArtifactUser.class, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords()
        );
    }

    @Override
    public List<ArtifactUser> list(ArtifactUserQuery artifactUserQuery) {
        MPJLambdaWrapper<ArtifactUserDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(ArtifactUserDO.class)
                .eq(ArtifactUserDO::getArtifactUuid, StrUtil.isNotBlank(artifactUserQuery.getArtifactUuid()) ? artifactUserQuery.getArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);
        wrapper.and(StrUtil.isNotBlank(artifactUserQuery.getCondition()), wp -> wp
                        .like(ArtifactUserDO::getArtifactUserName, artifactUserQuery.getCondition()).or()
                        .like(ArtifactUserDO::getArtifactRoleCode, artifactUserQuery.getCondition()).or()
                        .like(ArtifactUserDO::getArtifactType, artifactUserQuery.getCondition()))
                .apply(StrUtil.isNotBlank(artifactUserQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactUserQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactUserQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactUserQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        return artifactUserMapper.selectJoinList(ArtifactUser.class, wrapper);
    }


    @Override
    public Boolean save(ArtifactUser artifactUser) {
        return artifactUserMapper.insert(ArtifactUserToArtifactUserDOConverter.INSTANCE.artifactUserToArtifactUserDO(artifactUser)) == 1;
    }

    @Override
    public Boolean update(ArtifactUser artifactUser) {
        return artifactUserMapper.update(
                ArtifactUserToArtifactUserDOConverter.INSTANCE.artifactUserToArtifactUserDO(artifactUser),
                Wrappers.lambdaQuery(ArtifactUserDO.class).eq(ArtifactUserDO::getId, artifactUser.getId())) == 1;
    }

    @Override
    public Boolean delete(String id) {
        return artifactUserMapper.delete(Wrappers.lambdaQuery(ArtifactUserDO.class)
                .eq(ArtifactUserDO::getId, id)) == 1;
    }

    @Override
    public ArtifactUser query(String id) {
        ArtifactUserDO artifactUserDO = artifactUserMapper.selectOne(Wrappers.lambdaQuery(ArtifactUserDO.class).eq(ArtifactUserDO::getId, id));
        return ArtifactUserToArtifactUserDOConverter.INSTANCE.artifactUserDOToArtifactUser(artifactUserDO);
    }

    @Override
    public Boolean batchInsertArtifactUser(List<ArtifactUser> list) {
        return artifactUserMapper.insertBatch(list.stream().map(ArtifactUserToArtifactUserDOConverter.INSTANCE::artifactUserToArtifactUserDO).collect(Collectors.toList())) > 0;
    }

    @Override
    public Boolean deleteByArtifactUuid(String artifactUuid) {
        return artifactUserMapper.delete(Wrappers.lambdaQuery(ArtifactUserDO.class).eq(ArtifactUserDO::getArtifactUuid, artifactUuid)) == 1;
    }

    @Override
    public ArtifactUser queryArtifactUser(String userId, String artifactToolUuid) {
        ArtifactUserDO artifactUserDO = artifactUserMapper.selectOne(Wrappers.lambdaQuery(ArtifactUserDO.class).eq(ArtifactUserDO::getUserId, userId).eq(ArtifactUserDO::getArtifactUuid, artifactToolUuid));
        return ArtifactUserToArtifactUserDOConverter.INSTANCE.artifactUserDOToArtifactUser(artifactUserDO);
    }

    @Override
    public ArtifactUser queryArtifactUserByArtifactUserIdAndToolUuid(String pushUser, String artifactUuid) {
        /*MPJLambdaWrapper<ArtifactUserDO> wrapper = new MPJLambdaWrapper<>();
        wrapper.eq(ArtifactUserDO::getArtifactUserId, pushUser);
        wrapper.eq(ArtifactUserDO::getArtifactUuid, artifactUuid);*/
        ArtifactUserDO artifactUserDO = artifactUserMapper.selectOne(Wrappers.lambdaQuery(ArtifactUserDO.class)
                .eq(ArtifactUserDO::getArtifactUserId, pushUser)
                .eq(ArtifactUserDO::getArtifactUuid, artifactUuid));
        return ArtifactUserToArtifactUserDOConverter.INSTANCE.artifactUserDOToArtifactUser(artifactUserDO);
    }

}
